package breeze.pixel.weather.activity;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.card.MaterialCardView;

import java.util.Random;

import breeze.pixel.weather.App;
import breeze.pixel.weather.R;
import breeze.pixel.weather.apps_util.utils.MToast;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;
import breeze.pixel.weather.apps_util.views.WebActivity;
import brz.breeze.app_utils.BAppUtils;
import brz.breeze.payment.BPayTools;

public class DonateActivity extends AppCompatActivity {
    private static final String TAG = "DonateActivity";

    private final AppSettings settings = App.getAppSettings();
    private final Bundle donateData = getDonateData();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_donate);
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        MaterialCardView materialCardView = findViewById(R.id.donate_info);
        MaterialButton materialButton = findViewById(R.id.donate_button);
        Intent intent = getIntent();
        if (intent.getAction()!=null && intent.getAction().equals(Intent.ACTION_VIEW)){
            Uri data = intent.getData();
            if (data!=null){
                if (data.getQueryParameter("code")!=null && data.getQueryParameter("code").equals("1")){
                    settings.setDonated(true);
                }
            }
        }
        if (settings.isDonated()) {
            materialCardView.setVisibility(View.VISIBLE);
            materialButton.setVisibility(View.GONE);
        }else {
            materialCardView.setVisibility(View.GONE);
            materialButton.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        if (itemId == android.R.id.home){
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private static native Bundle getDonateData();

    public void pay(View view) {
        BAppUtils.execute(() -> {
            BPayTools bPayTools = new BPayTools();
            bPayTools.setOrderNo(String.valueOf(System.currentTimeMillis())+new Random().nextInt(999999));
            bPayTools.setNotifyUrl(donateData.getString("notice"));
            bPayTools.setReturnUrl(donateData.getString("return"));
            bPayTools.setAmount(donateData.getString("amount"));
            bPayTools.pay(new BPayTools.onPayResultListner() {
                @Override
                public void createOrderFail(String s) {
                    BAppUtils.uiExecute(() -> MToast.show(DonateActivity.this,"创建支付失败:"+s));
                }

                @Override
                public void createOrderSuccess(String s) {
                    WebActivity.startActivity(DonateActivity.this,s);
                }
            });
        });
    }

    public void eliminate(View view) {
        settings.setDonated(false);
    }
}
